﻿#include <QSharedMemory>
#include <QDesktopWidget>
#include <QScreen>

#include "application.h"
#include "mainwindow.h"
#include "dialog/dlglevel.h"
#include "dialog/dlgsystemset.h"

int main(int argc, char *argv[])
{
    Application app(argc, argv);

    // 只允许一个程序实例
    QSharedMemory memory("Servo");
    if (!memory.create(1))
        return 0;

    // 数据库访问接口
    DatabaseInterface databaseInterface(app.sqlDatabase());

    MainWindow win(app.settings(), &databaseInterface);
    //win.show();

    // 将窗口移动至屏幕中心
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QDesktopWidget *desktop = QApplication::desktop();
    int currentScreen = desktop->screenNumber(&win);
    QList<QScreen*> screenList = QGuiApplication::screens();
    QScreen *screen = screenList.at(currentScreen);
    win.move((screen->geometry().width() - win.width()) / 2, (screen->geometry().height() - win.height()) / 2);
#endif


    // 单元测试 测试级别选择
    DlgLevel dlgLevel(app.settings());
    dlgLevel.show();

    // 系统设置
    DlgSystemSet dlgSystemSet(app.settings());
    QObject::connect(&dlgLevel, &DlgLevel::accepted, &dlgSystemSet, &DlgSystemSet::onShow);
    QObject::connect(&dlgLevel, &DlgLevel::prepare, &win, &MainWindow::onPrepare);
    QObject::connect(&dlgSystemSet, &DlgSystemSet::accepted, &win, &MainWindow::onPrepareShow);


    return app.exec();
}
